<template>
  <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <el-form-item label="密码" prop="pass">
      <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="确认密码" prop="checkPass">
      <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button @click="go_back">返回</el-button>
      <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
    //导入cmsApi中的所有接口
    import * as userApi from '../api/user'

    export default {
        data() {
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.ruleForm.checkPass !== '') {
                        this.$refs.ruleForm.validateField('checkPass');
                    }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.ruleForm.pass) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return {
                userId: '',
                ruleForm: {
                    pass: '',
                    checkPass: ''
                },
                rules: {
                    pass: [
                        { validator: validatePass, trigger: 'blur' }
                    ],
                    checkPass: [
                        { validator: validatePass2, trigger: 'blur' }
                    ]
                }
            };

        },
        methods: {
            //返回主页面调用的方法
            go_back() {
                //设置路由
                this.$router.push({
                    path: '/system/user/list',
                    //设置路由携带的参数为当前路由中的参数
                    query: {
                        page: this.$route.query.page,
                    }
                })
            },submitForm(formName) {
                //进行密码校验
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                       //提交密码修改请求
                        this.editSubmit();
                    } else {
                        console.log('错误提交!!');
                        return false;
                    }
                });
            },
            //提交修改密码
            editSubmit() {
                this.$confirm('确认修改密码吗？', '提示', {}).then(() => {
                    this.addLoading = true;
                    userApi.user_editPwd(this.userId, this.ruleForm.pass).then((res) => {
                        console.log(res);
                        if (res.success) {
                            this.addLoading = false;
                            this.$message({
                                message: '修改成功',
                                type: 'success'
                            });
                            //返回主页面
                            this.go_back();
                        } else {
                            this.addLoading = false;
                            if (res.message) {
                                this.$message.error(res.message);
                            } else {
                                this.$message.error('提交失败');
                            }
                        }
                    });
                });
            },
        },
        //钩子方法，页面创建渲染之前调用
        created: function () {
            //从url中获取pageId,params指的是从url中获取指定参数
            this.userId = this.$route.params.userId;
        },
        //钩子方法，在页面渲染之后调用
        mounted: function () {

        }
    }
</script>
<style>

</style>
